ruby:
  require_locals ['f'], local_assigns
  item                ||= f.object
  image_name          ||= :image
  the_image             = item.send(image_name)
  label               ||= image_name.to_s.titleize
  alt_label           ||= "#{label} Alt Text"
  caption_label       ||= "#{label} Caption"
  helper_text         ||= ""
  alt_helper_text     ||= t('fae.images.alt_helper')
  caption_helper_text ||= ""
  show_alt              = true if show_alt.nil?
  show_caption        ||= false
  required            ||= false
  attached_as         ||= image_name.to_s
  languages             = f.object.class.try(:fae_fields).try(:[], image_name).try(:[], :languages)
  language            ||= nil
  show_form_manager   ||= true

- if languages.present?
  - languages.each do |lang|
    = render 'fae/images/image_uploader', f: f, item: item, image_name: "#{image_name}_#{lang}".to_sym, label: "#{label} (#{lang.to_s})", alt_label: "#{alt_label} (#{lang.to_s})", caption_label: "#{caption_label} (#{lang.to_s})", helper_text: helper_text, alt_helper_text: alt_helper_text, caption_helper_text: caption_helper_text, show_caption: show_caption, required: required, attached_as: "#{attached_as}_#{lang.to_s}", language: lang, show_form_manager: show_form_manager

- else
  ruby:
    if Fae.languages.keys.any? { |lang| image_name.to_s.end_with?(lang.to_s) } && !language.present?
      label = label.split
      lang_for_label = "(#{label.pop()})"
      label = label.push(lang_for_label).join(" ")
      wrapper_html = { data: { language: image_name.to_s.split("_").last.to_sym } }
    elsif language.present?
      wrapper_html = { data: { language: language } }
    else
      wrapper_html = {}
    end
    form_manager_id = show_form_manager ? "#{item.class.name}_#{attached_as}" : nil
  = f.simple_fields_for image_name, defaults: { wrapper_html: wrapper_html } do |i|
    .input.field.image*wrapper_html class="#{image_name}" data-form-manager-id=form_manager_id
      label class="#{'required' if required}"
        - if required
          abbr title="required" *

        = " #{label.html_safe}"

        - if helper_text.present?
          h6.helper_text
            span.helper_text_text = helper_text

      - if the_image.asset.present?
        .asset-actions
          img.js-image-modal src=the_image.asset.url
          .asset-title = the_image.asset.file.filename

          = link_to '', fae.delete_image_path(the_image.id), class: 'asset-delete js-asset-delete', remote: true, method: :delete, data: { confirm: t('fae.delete_confirmation') } if the_image.id.present?

      .asset-inputs style="#{'display:none;' if the_image.asset.present?}"
        = i.input :asset, as: :file, label: false, input_html: { data: { limit: Fae.max_image_upload_size, exceeded: t('fae.exceeded_upload_limit') } }
        = i.input :asset_cache, as: :hidden
        - imageable_type = f.object.class.superclass.name == 'Fae::StaticPage' ? 'Fae::StaticPage' : f.object.class.name
        = i.input :imageable_type, as: :hidden, input_html: { value: imageable_type }
        = i.input :attached_as, as: :hidden, input_html: { value: attached_as }

    - if show_caption
      ruby:
        options = { label: caption_label.html_safe }
        options[:helper_text] = caption_helper_text if caption_helper_text.present?
        options[:wrapper_html] = wrapper_html
        options[:wrapper_html].merge!({class: "#{form_manager_id}_caption_container"})
        options[:show_form_manager] = false

      = fae_input i, :caption, options

    - if show_alt
      ruby:
        options = { label: alt_label.html_safe }
        options[:helper_text] = alt_helper_text if alt_helper_text.present?
        options[:wrapper_html] = wrapper_html
        options[:wrapper_html].merge!({class: "#{form_manager_id}_alt_container"})
        options[:show_form_manager] = false

        if (Fae::Option.instance.translate_language && !form_manager_id.to_s.end_with?('_en') && Fae.languages.keys.any? { |field| form_manager_id.to_s.include?(field.to_s) })
          translate = content_tag(:span, 'Translate', class: "button js-translate-button translate-button")
          options[:translate] = translate.html_safe if translate.present?
        end 
        
        if open_ai_enabled?
          button_options = {
            class: "button js-generate-alt-button-on-form generate-alt-button", 
            data: { 
              image_name: "#{item.class.name.underscore}_#{image_name}",
              image_id: the_image.id
            }
          }
          generate_alt = content_tag(:button, 'Generate Alt', button_options)
          options[:generate_alt] = generate_alt.html_safe if generate_alt.present?
        end

      = fae_input i, :alt, options

    = i.hidden_field :required, value: required
